﻿Imports System.Data.OleDb

Public Class frmCloreLocation

    Dim bdVehicule As New cls_GestionBD("taco.mdb")
    Dim bdCategories As New cls_GestionBD("taco.mdb")
    Dim bdLocation As New cls_GestionBD("taco.mdb")

    Dim valeurVehicule As Integer
    Dim locatio As clsLocation

    Public Overloads Function ShowDialog(ByRef lo As clsLocation) As Windows.Forms.DialogResult
        locatio = lo

        With locatio.vehicule
            txtNoImmatriculation.Text = .noImmatriculation
            cboCategories.Text = .categorie.nomCategorie
            cboMarque.Text = .marque
            cboModele.Text = .modele
            txtAnnee.Text = .annee
            updValeur.Value = .valeur
            txtKilometrage.Text = .kilometrage
            updNbrPlaces.Value = .nbrPlaces
            txtCapaciteChargement.Text = .capaciteChargement
            cboTransmission.Text = .transmission
            txtCommentaires.Text = .commentaire
            lblPrixCategorie.Text = .categorie.coutLocationJour



            lblSurchargeAssurance.Text = .categorie.assurance
            cboEtatReservoirApres.Text = "100 % plein"
            dtpDateRetour.Value = FormatDateTime(Now, DateFormat.ShortDate)
            dtpDateRetour.MinDate = locatio.dateDebut

        End With

        updKilometrage.Minimum = locatio.nbrKmDepart
        updKilometrage.Value = locatio.nbrKmDepart

        cboModeDePaiement.Text = locatio.modePaiementDepot
        txtNumeroDeCarte.Text = locatio.noCarteDepot
        txtNomSurLaCarte.Text = locatio.nomCarteDepot

        'load up order defaults
        Return MyBase.ShowDialog()
    End Function

    'Private Sub frmCloreLocation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    '    bdLocation.remplirDataSet("select * from tblLocations where noLocation = " & noLocation, "tblLocation")

    '    'remplit les informations sur le véhicule
    '    bdVehicule.daBd.SelectCommand = New OleDbCommand("SELECT * FROM tblVehicules WHERE noVehicule = @noVehicule")
    '    bdVehicule.daBd.SelectCommand.Parameters.Add("@noVehicule", OleDbType.Integer).Value = noVehicule

    '    bdVehicule.remplirDataSet("tblVehicule")

    '    With bdVehicule.dsBd.Tables("tblVehicule").Rows(0)
    '        txtNoImmatriculation.Text = .Item("noImmatriculation")
    '        cboMarque.Text = nvl(.Item("marque"), "")
    '        cboModele.Text = nvl(.Item("modele"), "")
    '        txtAnnee.Text = nvl(.Item("annee"), "")
    '        updValeur.Value = valeurVehicule = nvl(.Item("valeur"), 0)
    '        txtKilometrage.Text = nvl(.Item("kilometrage"), 0)
    '        updNbrPlaces.Value = nvl(.Item("nbrPlaces"), 0)
    '        txtCapaciteChargement.Text = nvl(.Item("capaciteChargement"), 0)
    '        cboTransmission.Text = nvl(.Item("transmission"), "")
    '        txtCommentaires.Text = nvl(.Item("commentaire"), "")
    '        updKilometrage.Minimum = updKilometrage.Value = nvl(.Item("kilometrage"), 0)
    '    End With

    '    bdCategories.daBd.SelectCommand = New OleDbCommand("SELECT * FROM tblCategories WHERE noCategorie = @noCategorie")
    '    bdCategories.daBd.SelectCommand.Parameters.Add("@noCategorie", OleDbType.VarChar, 15).Value = bdVehicule.dsBd.Tables("tblVehicule").Rows(0).Item("noCategorie")
    '    bdCategories.remplirDataSet("tblCategorie")

    '    With bdCategories.dsBd.Tables("tblCategorie").Rows(0)
    '        cboCategories.Text = nvl(.Item("nomCategorie"), "")
    '        lblSurchargeAssurance.Text = CDbl(nvl(.Item("assurance"), 0))
    '    End With

    '    cboEtatReservoirApres.Text = "100 % plein"

    '    'Dim test As New Date(Now.Year, Now.Month, Now.Day)
    '    dtpDateRetour.Value = New Date(Now.Year, Now.Month, Now.Day)
    '    'dtpDateRetour.MinDate = New Date(Now.Year, Now.Month, Now.Day)

    'End Sub

    Private Sub OptAccidentOui_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optAccidentOui.CheckedChanged
        If optAccidentOui.Checked Then
            optAccidentResponsableNon.Enabled = True
            optAccidentResponsableOui.Enabled = True
        Else
            optAccidentResponsableNon.Enabled = False
            optAccidentResponsableOui.Enabled = False
        End If
    End Sub

    Private Sub btnEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnregistrer.Click

        If optAccidentOui.Checked Then

            Dim bd As New cls_GestionBD("taco.mdb")
            bd.remplirDataSet("SELECT * FROM tblAccidents", "tblAccidents")

            Dim lNewRow As DataRow
            lNewRow = bd.dsBd.Tables("tblAccidents").NewRow

            With lNewRow
                .Item("noVehicule") = locatio.vehicule.noVehicule
                .Item("noClient") = locatio.noClient
                .Item("dateAccident") = FormatDateTime(Now, DateFormat.ShortDate)
                .Item("accidentResponsable") = optAccidentResponsableOui.Checked
            End With

            bd.dsBd.Tables("tblAccidents").Rows.Add(lNewRow)
            bd.miseAJourBD("tblAccidents")

            verifierListeNoir(locatio.noClient)

        End If


        With locatio
            .dateVehiculeRemis = FormatDateTime(dtpDateRetour.Value, DateFormat.ShortDate)
            .etatReservoir = cboEtatReservoirApres.Text.Substring(0, 3)
            .nbrKmRetour = updKilometrage.Value
            .actif = False

            .vehicule.kilometrage = txtKilometrage.Text

            .facture.modePaiement = cboModeDePaiement.Text
            .facture.noCarte = txtNumeroDeCarte.Text
            .facture.dateExpirationCarte = dtpDateExpiration.Value
            .facture.sousTotal = lblSousTotal.Text
            .facture.tps = lblTPS.Text
            .facture.tvq = lblTVQ.Text
            .facture.actif = True
        End With

        locatio.enregistrer()
        frmGestionLocations.btnChercher.PerformClick()


        'With bdLocation.dsBd.Tables("tblLocation").Rows(0)
        '    .Item("dateVehiculeRemis") = dtpDateRetour.Value
        '    .Item("etatReservoirApres") = CDbl(cboEtatReservoirApres.Text.Substring(0, 3))
        '    .Item("nbrKmRetour") = updKilometrage.Value
        '    .Item("actif") = False
        'End With
        'bdLocation.miseAJourBD("tblLocation")

        'With bdVehicule.dsBd.Tables("tblVehicule").Rows(0)
        '    .Item("kilometrage") = txtKilometrage.Text
        'End With
        'bdVehicule.miseAJourBD("tblVehicule")

        'Dim bdFacture As New cls_GestionBD("taco.mdb")
        'bdFacture.remplirDataSet("select * from tblFactures", "tblFacture")
        'Dim lNewRow As DataRow
        'lNewRow = bdFacture.dsBd.Tables("tblFacture").NewRow
        'With lNewRow
        '    .Item("noFacture") = createNewNo("noFacture", "tblFactures")
        '    .Item("noLocation") = noLocation
        '    .Item("typePaiement") = cboModeDePaiement.Text
        '    .Item("noCarte") = txtNumeroDeCarte.Text
        '    .Item("dateExpirationCarte") = dtpDateExpiration.Value
        '    .Item("sousTotal") = lblSousTotal.Text
        '    .Item("tps") = lblTPS.Text
        '    .Item("tvq") = lblTVQ.Text
        '    .Item("actif") = True
        'End With
        'bdFacture.dsBd.Tables("tblFacture").Rows.Add(lNewRow)
        'bdFacture.miseAJourBD("tblFacture")

        Me.Close()

    End Sub

    Private Sub cboEtatReservoirApres_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboEtatReservoirApres.SelectedIndexChanged
        If cboEtatReservoirApres.Text <> "100 % plein" Then
            lblSurchargeReservoir.Text = getParam("surchargeReservoir")
        Else
            lblSurchargeReservoir.Text = 0
        End If
    End Sub

    Private Sub dtpDateRetour_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpDateRetour.ValueChanged

        With locatio
            Dim calcul As TimeSpan = dtpDateRetour.Value - CDate(.dateDebut)
            lblPrixVehicule.Text = CDbl(.vehicule.categorie.coutLocationJour * (Math.Round(calcul.TotalDays) + 1))

            calcul = dtpDateRetour.Value - CDate(.dateFin)

            If CDbl(Math.Round(calcul.TotalDays)) = 1 Then
                lblSurchargeRetard.Text = CDbl(getParam("coutRetardJour1") + lblPrixVehicule.Text)
            End If

            If CDbl(Math.Round(calcul.TotalDays)) > 1 Then
                lblSurchargeRetard.Text += getParam("coutRetardAutreJour") * CDbl(Math.Round(calcul.TotalDays))
            End If

            If CDbl(Math.Round(calcul.TotalDays)) >= getParam("jourPerteDepot") Then
                lblSurchargeRetard.Text += getParam("prcDepotPerduRetard") * .montantDepot
            End If

            If CDbl(Math.Round(calcul.TotalDays)) >= 0 Then
                lblSurchargeRetardNom.Text = "Surcharge retard (" & CDbl(Math.Round(calcul.TotalDays)) & " j.) :"
            Else
                lblSurchargeRetardNom.Text = "Surcharge retard (0 j.) :"
            End If
        End With

        dtpDateExpiration.MinDate = FormatDateTime(dtpDateRetour.Value, DateFormat.ShortDate)
        dtpDateExpiration.Value = FormatDateTime(dtpDateRetour.Value, DateFormat.ShortDate)

    End Sub

    Private Sub updKilometrage_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updKilometrage.ValueChanged
        With locatio
            Dim nbrKm As Integer = (updKilometrage.Value - .nbrKmDepart) - .vehicule.categorie.kmAvantSurcharge

            If nbrKm > 0 Then
                lblSurchargeKilometrage.Text = nbrKm * .vehicule.categorie.coutSurchargeKm
                lblSurchargeKilometrageNom.Text = "Surcharge kilométrage (" & nbrKm & " km) :"
            Else
                lblSurchargeKilometrage.Text = 0
                lblSurchargeKilometrageNom.Text = "Surcharge kilométrage (0 km) :"
            End If
        End With
    End Sub

    Private Sub lblPrixVehicule_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblSurchargeRetard.TextChanged, lblSurchargeReservoir.TextChanged, lblSurchargeKilometrage.TextChanged, lblSurchargeAssurance.TextChanged, lblPrixVehicule.TextChanged
        If lblSurchargeReservoir.Text <> "" AndAlso lblPrixVehicule.Text <> "" AndAlso lblSurchargeRetard.Text <> "" AndAlso lblSurchargeKilometrage.Text <> "" AndAlso lblSurchargeAssurance.Text <> "" Then

            lblSousTotal.Text = CDbl(nvl(lblSurchargeReservoir.Text, 0)) + _
            nvl(lblPrixVehicule.Text, 0) + _
            nvl(lblSurchargeRetard.Text, 0) + _
            nvl(lblSurchargeKilometrage.Text, 0) + _
            nvl(lblSurchargeAssurance.Text, 0)
        End If

    End Sub

    Private Sub lblSousTotal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblSousTotal.TextChanged
        If lblSousTotal.Text <> "" Then
            lblTPS.Text = FormatNumber(getParam("tps") * CDbl(lblSousTotal.Text), 2)
            lblTVQ.Text = FormatNumber(getParam("tvq") * (CDbl(lblSousTotal.Text) + CDbl(lblTPS.Text)), 2)
            lblTotal.Text = FormatNumber(CDbl(lblSousTotal.Text) + CDbl(lblTPS.Text) + CDbl(lblTVQ.Text), 2)
        End If
    End Sub

    Private Sub btnAnnuler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnnuler.Click
        Me.Close()
    End Sub

    Private Sub verifierListeNoir(ByVal noClient As Integer)

        Dim bd As New cls_GestionBD("taco.mdb")
        Dim cmd As New OleDbCommand()

        cmd.CommandText = _
            "SELECT nbrRetards, nbrReservoirsNonPleins " & _
            "FROM tblClients " & _
            "WHERE noClient = @noClient"
        cmd.Parameters.Add("@noClient", OleDbType.Integer)
        cmd.Parameters("@noClient").Value = noClient

        bd.remplirDataSet(cmd, "tblClient")

        With bd.dsBd.Tables("tblClient").Rows(0)

            If .Item("nbrRetards") > getParam("nbRetardBlackList") AndAlso optAccidentOui.Checked Then

                'ajouter ajouter le client sur la liste noir
                Dim bdListeNoir As New cls_GestionBD("taco.mdb")
                bdListeNoir.remplirDataSet("SELECT * FROM tblListeNoire", "tblListeNoir")

                Dim lNewRow As DataRow
                lNewRow = bdListeNoir.dsBd.Tables("tblListeNoir").NewRow

                With lNewRow
                    .Item("dateDebut") = FormatDateTime(Now, DateFormat.ShortDate)
                    .Item("noClient") = locatio.noClient
                    .Item("duree") = getParam("dureeListeNoire")
                    .Item("commentaire") = InputBox("Le client a trop de retard, il sera mit sur la liste noir pour une durée de " & getParam("dureeListeNoire") & " jours. Entrez un commentaire si nécessiare")
                End With

                bdListeNoir.dsBd.Tables("tblListeNoir").Rows.Add(lNewRow)

                'met à jour la base de données
                bdListeNoir.miseAJourBD("tblListeNoir")

                .Item("nbrRetards") = 0

            End If

            If .Item("nbrReservoirsNonPleins") > getParam("nbFauteReservoirBlackList") AndAlso cboEtatReservoirApres.Text <> "100 % plein" Then

                'ajouter ajouter le client sur la liste noir
                'ajouter ajouter le client sur la liste noir
                Dim bdListeNoir As New cls_GestionBD("taco.mdb")
                bdListeNoir.remplirDataSet("SELECT * FROM tblListeNoire", "tblListeNoir")

                Dim lNewRow As DataRow
                lNewRow = bdListeNoir.dsBd.Tables("tblListeNoir").NewRow

                With lNewRow
                    .Item("dateDebut") = FormatDateTime(Now, DateFormat.ShortDate)
                    .Item("noClient") = locatio.noClient
                    .Item("duree") = getParam("dureeListeNoire")
                    .Item("commentaire") = InputBox("Le client a trop remit trop de véhicules avec le réservoir non plein, il sera mit sur la liste noir pour une durée de " & getParam("dureeListeNoire") & " jours. Entrez un commentaire si nécessiare")
                End With

                bdListeNoir.dsBd.Tables("tblListeNoir").Rows.Add(lNewRow)

                'met à jour la base de données
                bdListeNoir.miseAJourBD("tblListeNoir")

                .Item("nbrReservoirsNonPleins") = 0

            End If

        End With



    End Sub

    Private Sub frmCloreLocation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub cboModeDePaiement_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboModeDePaiement.SelectedIndexChanged
        Select Case cboModeDePaiement.Text
            Case "Carte de crédit"
                lblNumeroSurLaCarte.Visible = True
                txtNumeroDeCarte.Visible = True
                lblNomSurLaCarte.Visible = True
                txtNomSurLaCarte.Visible = True
                lblDateExpiration.Visible = True
                dtpDateExpiration.Visible = True

            Case "Carte de débit"
                lblNumeroSurLaCarte.Visible = False
                txtNumeroDeCarte.Visible = False
                lblNomSurLaCarte.Visible = False
                txtNomSurLaCarte.Visible = False
                lblDateExpiration.Visible = False
                dtpDateExpiration.Visible = False

            Case "Comptant"
                lblNumeroSurLaCarte.Visible = False
                txtNumeroDeCarte.Visible = False
                lblNomSurLaCarte.Visible = False
                txtNomSurLaCarte.Visible = False
                lblDateExpiration.Visible = False
                dtpDateExpiration.Visible = False
        End Select

        txtNumeroDeCarte.Clear()
        txtNomSurLaCarte.Clear()

    End Sub

End Class